package cn.bjc.test2;

import cn.bjc.utils.ESClientUtil;
import org.elasticsearch.action.delete.DeleteRequestBuilder;
import org.elasticsearch.action.get.GetRequestBuilder;
import org.elasticsearch.action.index.IndexRequestBuilder;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.update.UpdateRequestBuilder;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.sort.SortOrder;
import org.junit.Test;

import java.util.HashMap;

public class EsTest2 {
    //新增一个文档
    @Test
    public void testAdd(){
        //1.得到一个ES链接
        TransportClient client = ESClientUtil.getClient();
        
        //2.新建对象,指定索引,类型,id
        IndexRequestBuilder requestBuilder = client.prepareIndex("pethome", "pet", "1");
        
        //新建map,设置进数据
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("id", 1L);
        hashMap.put("name", "二哈");
        hashMap.put("age", 1);
        IndexRequestBuilder result = requestBuilder.setSource(hashMap);
        System.out.println(result.get());
        
        //关流
        client.close();

    }
    
    //获得一个文档
    @Test
    public void testGet(){
        //1.得到一个ES链接
        TransportClient client = ESClientUtil.getClient();
        
        //2.设置要获取的对象的索引,类型,id
        GetRequestBuilder builder = client.prepareGet("pethome", "pet", "1");
        
        //输出测试
        System.out.println(builder.get().getSourceAsMap());
        
        //3.关流
        client.close();
    }
    
    //修改一个文档
    @Test
    public void testUpdate(){
        //1.得到一个ES链接
        TransportClient client = ESClientUtil.getClient();
        
        //2.设置要修改的对象的索引,类型,id
        UpdateRequestBuilder updateRequestBuilder = client.prepareUpdate("pethome", "pet", "1");
        
        //3.新建map,放入要修改的数据
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("id", 2L);
        hashMap.put("name", "金毛狮王");
        hashMap.put("age", 0);
        
        //将map数据放入
        UpdateResponse updateResponse = updateRequestBuilder.setDoc(hashMap).get();

        System.out.println(updateResponse);

        //关流
        client.close();
    }
    
    //删除一个文档
    @Test
    public void testDelete(){
        //1.得到一个ES链接
        TransportClient client = ESClientUtil.getClient();
        
        //要删除的索引,类型,id
        DeleteRequestBuilder deleteRequestBuilder = client.prepareDelete("pethome", "pet", "1");

        System.out.println(deleteRequestBuilder.get());
        
        //关流
        client.close();
    }
    
    //批量写入测试数据
    @Test
    public void testPatch(){
        //1.得到一个ES链接
        TransportClient client = ESClientUtil.getClient();
        
        //2.批量创建文档并写入数据
        for (int i = 1; i < 50; i++) {
            IndexRequestBuilder indexRequestBuilder = client.prepareIndex("pethome", "pet", i+"");
            
            //批量新建hashmap并放入数据
            HashMap<String, Object> hashMap = new HashMap<>();
            hashMap.put("id", i);
            hashMap.put("name", "柯基"+ i);
            hashMap.put("sex", i%2);
            hashMap.put("age", i);
            IndexRequestBuilder requestBuilder = indexRequestBuilder.setSource(hashMap);
            System.out.println(requestBuilder.get());
        }
        //关流
        client.close();

    }
    
    //综合案例
    /**
     * 综合案例
     * - 查询用户表
     * - name包含：柯基
     * - age在1~12之间
     * - sex=1
     * - 每页大小2
     * - 从2页开始查
     * - 按照age倒序
     */

    @Test
    public void test(){
        //1.得到一个ES链接
        TransportClient client = ESClientUtil.getClient();
        
        //2.设置要查询的索引库
        SearchRequestBuilder searchRequestBuilder = client.prepareSearch("pethome");
        
        //3.得到bool对象
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        
        //4.设置match条件
        MatchQueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("name", "柯基");
        boolQueryBuilder.must(matchQueryBuilder);
        
        //5.设置过滤条件
        boolQueryBuilder
                .filter(QueryBuilders.termQuery("sex", 1))
                .filter(QueryBuilders.rangeQuery("age").gte(1).lte(12));
        
        //6.设置,查询内容
        searchRequestBuilder.setQuery(boolQueryBuilder);

        //7.设置分页条件
        searchRequestBuilder.setSize(2);
        searchRequestBuilder.setFrom(2);
        
        //8.设置排序规则
        searchRequestBuilder.addSort("age", SortOrder.DESC);
        
        //9.得到查询结果
        SearchHit[] hits = searchRequestBuilder.get().getHits().getHits();
        
        //10.打印出查询结果
        for (SearchHit hit : hits) {
            System.out.println(hit.getSourceAsMap());
        }

        //关流
        client.close();
    }
    
    
    
}
